﻿using System;
using Com.OpenCode.Framework.Core;
using DevExpress.XtraBars;
using DevExpress.XtraEditors.Repository;

namespace Com.OpenCode.Framework.DevForm
{
    /// <summary>
    ///     日期。
    /// </summary>
    public class StatusDateEditUI : BarEditItem, IStatusSetValue
    {
        #region 字段

        private BarItemLink _BarItemLink;

        #endregion

        private int _InformationIndex = -1;

        #region 构造函数

        public StatusDateEditUI(BuildItemEventArgs args)
        {
            args.Plugin.Owner = this;
            StatusBarServices.Register += StatusHelper_Register;
            Disposed += StatusLabelUI_Disposed;
            Width = args.Plugin.Properties.GetInt("Width");
            _InformationIndex = args.Plugin.Properties.GetInt("InformationIndex");
            RepositoryItemDateEdit _RepositoryItemDateEdit = new RepositoryItemDateEdit();
            Edit = _RepositoryItemDateEdit;
            _BarItemLink = (args.Owner as StatusBarUI).ItemLinks.Add(this);
        }

        #endregion

        #region 事件方法

        public void SetStatusValue(object value)
        {
            DateTime _DateTime = DateTime.Now;
            DateTime.TryParse(value.ToString(), out _DateTime);
            EditValue = _DateTime;
        }

        private void StatusLabelUI_Disposed(object sender, EventArgs e)
        {
            StatusBarServices.Register -= StatusHelper_Register;
        }

        private void StatusHelper_Register(EditStatusValueArgs e)
        {
            if (e.Index == InformationIndex)
            {
                SetStatusValue(e.Statusvalue);
                SetStatusVisible(e.Visible);
            }
        }


        public void SetStatusVisible(bool visible)
        {
            if (_BarItemLink != null)
            {
                _BarItemLink.Visible = visible;
            }
        }

        public int InformationIndex
        {
            get { return _InformationIndex; }
        }

        #endregion
    }
}